package org.black.phoneUtility.sms.reply.app.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.black.phoneUtility.sms.reply.provider.SmsReplyContract;

import android.app.IntentService;
import android.appwidget.AppWidgetManager;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class SmsReplyAppWidgetDeleteIntentService extends IntentService {
	private static final String LOG_TAG = SmsReplyAppWidgetDeleteIntentService.class
			.getName();
	public static final String ARGUMENTS_SMS_REPLY_ID = "arguments_sms_reply_id";

	public SmsReplyAppWidgetDeleteIntentService() {
		super(SmsReplyAppWidgetDeleteIntentService.class.getName());
	}

	@Override
	public void onCreate() {
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		int appWidgetIds[] = null;
		Bundle extras = intent.getExtras();
		if (extras != null) {
			appWidgetIds = extras
					.getIntArray(AppWidgetManager.EXTRA_APPWIDGET_IDS);
		}
		if (appWidgetIds == null || appWidgetIds.length == 0) {
			Log.e(LOG_TAG, "appWidgetIds is empty!");
			return;
		}

		try {
			int appWidgetIdsLength = appWidgetIds.length;
			List<String> whereArgus = new ArrayList<String>();

			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.append("?");
			whereArgus.add(String.valueOf(appWidgetIds[0]));
			for (int i = 1; i < appWidgetIdsLength; i++) {
				stringBuilder.append(",?");
				whereArgus.add(String.valueOf(appWidgetIds[i]));
			}
			int deleteCount = this.getContentResolver().delete(
					SmsReplyContract.SmsReplyAppWidgetIds.CONTENT_URI,
					SmsReplyContract.SmsReplyAppWidgetIds.APP_WIDGET_ID
							+ " IN (" + stringBuilder.toString() + " )",
					whereArgus.toArray(new String[whereArgus.size()]));
			Log.d(LOG_TAG,
					"Delete " + deleteCount
							+ " SmsReplyAppWidget. appWidgetIds: "
							+ Arrays.toString(appWidgetIds));

		} catch (Throwable throwable) {
			Log.e(LOG_TAG, "Unknown Error!", throwable);
		}

	}
}
